# delimit ;
clear all; clear mata; graph drop _all;
set more off; set graphics off;
set maxvar 120000; set linesize 200;
cap log close; cap ssc install estout;
log using output/replication.log, replace;

/*	Do Managers Do Good with Other Peoples' Money?
	Cheng, Hong, and Shue, RCFS
	Dividend tax cut analysis				
	
	For ease, this code is designed to run with all data files (input), logs (output), and tables (output) in the same directory
	
	Figure 4 displays estimates reported in Table 2 and is thus not included */;
	
local file "input/dividendtaxcut";
local file_fig1 "input/kld_annual_scores";
local carfile "input/car_nq10_divtaxcut";
local rawretfile "input/rawret_nq10_divtaxcut";
local cartsfile "input/car_daily_nq5_ts_divtaxcut";
local uioxfam "input/uioxfam";
local esttabopt "se(3) b(3) star(* 0.1 ** 0.05 *** 0.01) brackets staraux mlabels(,titles)";

* table 1 -- summary stats;
	use `file', clear;

	* panel A;
	* variables: KLD, insider ownership, assets, cash / assets, leverage, dedicated institutional ownership calculated as 2001-2002 average for firms in 2002;
	tabstat kld_diff_nogov_rankingp2003 eg_slope_dd0_rankingp2003 at_rankingp2003 cash_rankingp2003 lev_rankingp2003 tfn_io_cls_DED_rankingp2003 if year == 2002 & mainperiod == 1, c(s) s(me sd min max p50 n) by(eg_slope_dd0_rankingp2003_xt) format(%9.2f);
	
	* panel B;
	* variables: dividend initiation or decrease, kld scores, environment, community, dividend, product scores;
	tabstat div_initincr_ind kld_diff_nogov env_diff com_diff div_diff pro_diff emp_diff if mainsamp == 1, c(s) s(me sd min max p50 n) by(post) format(%9.2f);

	* panel D kld-refinitiv;	
	pwcorr kld_diff_nogov ln_so_so_co_dp027 so_so_co_dp036 if mainsamp;	
	

	* generate residuals against year/firm FEs;
	local vars "kld_diff_nogov ln_so_so_co_dp027 so_so_co_dp036";
	foreach var of local vars {;
		quietly reg `var' i.year i.firmid if mainsamp;
		predict `var'_resid if mainsamp, resid;
	};
	
	eststo table1D_a: reg kld_diff_nogov ln_so_so_co_dp027 if mainsamp, vce(cluster firmid);
	eststo table1D_a_fe: reg kld_diff_nogov_resid ln_so_so_co_dp027_resid if mainsamp, vce(cluster firmid);
	
	eststo table1D_b: reg kld_diff_nogov so_so_co_dp036   if mainsamp, vce(cluster firmid);
	eststo table1D_b_fe: reg kld_diff_nogov_resid so_so_co_dp036_resid if mainsamp, vce(cluster firmid);

	esttab * using output/table1.csv, replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';

* table 2;
	eststo clear;

	use `file', clear;

	* for panel B, group 5/6 together;
	replace eg_slope_dd0_rankingp2003_xt1 = 5 if eg_slope_dd0_rankingp2003_xt1 == 6;

	eststo table2A: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt) if mainperiod, fe vce(cluster firmid);
	eststo table2B: xtreg kld_diff_nogov ib2002.year##(ib5.eg_slope_dd0_rankingp2003_xt1 ib5.at_rankingp2003_xt1) if mainperiod, fe vce(cluster firmid);
	
	esttab table2A, r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';
	esttab table2B, r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';
	
	esttab * using output/table2.csv, replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';
	

* table 3;
	* i = base year, j = shock/ranking year;
	eststo clear;
	forval i = 1994/1998 {;
		local j = `i' + 1;

		use `file', clear;
		drop mainperiod post;
		gen t = .;

		forval k = -3/4 {;
			* k indexes offset;
			* t indexes offset but excludes negative numbers for stata factor variable compatibility;
			* t = 3 for base year;
			local t = `k' + 3;
			replace t = `t' if year == `i'+`k';
		};

		gen mainperiod = inrange(year,`i'-3,`i'+4);
		gen post = (year > `i');
		local vars "eg_slope_dd0 at";
		foreach var of local vars {;
			gen `var'_rank_xt = `var'_rankingp`j'_xt;
		};

		eststo base`i': quietly xtreg kld_diff_nogov ib3.t##(ib3.eg_slope_dd0_rank_xt ib3.at_rank_xt) if mainperiod, fe vce(cluster firmid);
	};

	esttab * using output/table3.csv , replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';

* table 4;
	eststo clear;
	use `file', clear;
	
	* asset, leverage, cash;
	eststo table4_1: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod, fe vce(cluster firmid);
	
	* + dedicated institutional ownership ;
	eststo table4_2: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.tfn_io_cls_DED_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod, fe vce(cluster firmid);
	
	* + largest block;
	eststo table4_3: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.tfn_io_max_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod, fe vce(cluster firmid);

	* + max director ownership;
	eststo table4_4: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.max_dirown_i_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod, fe vce(cluster firmid);
	
	esttab * using output/table4.csv , replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';
	
* table 5;
	eststo clear;
	use `file', clear;

	local vars "com_diff div_diff emp_diff env_diff pro_diff";
	foreach var of local vars {;
		eststo c_`var': quietly xtreg `var' ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt) if mainperiod, fe vce(cluster firmid);
	};
	esttab * using output/table5.csv , replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';

* table 6;
	* panel A;
	use `uioxfam', clear;
	pwcorr max_ben_rank oxfam_score_rank oxfam_score_wage_rank oxfam_score_prot_rank oxfam_score_rtorg_rank if year == 2002; 

	
	* panels B, C;
	eststo clear;
	use `file', clear;
	
	eststo table6B_1: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod & max_ben_rankingp2003_xt3 == 3, fe vce(cluster firmid);
	eststo table6B_2: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod & max_ben_rankingp2003_xt3 == 1, fe vce(cluster firmid);
	eststo table6C_1: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod & oxfam_score_xt3 == 3, fe vce(cluster firmid);
	eststo table6C_2: xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt ib3.lev_rankingp2003_xt ib3.cash_rankingp2003_xt) if mainperiod & oxfam_score_xt3 == 1, fe vce(cluster firmid);
	
	esttab * using output/table6.csv , replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';
	
* table 7;
	* note that "mid" of 5 combines 5 and 6;
	* panel A;
	local mid 5;
	eststo clear;
	use `carfile', clear;
	eststo daily180: reg car_mkt180 ib`mid'.xt , vce(cluster firmid);
	eststo daily360: reg car_mkt360 ib`mid'.xt , vce(cluster firmid);
	eststo monthly24: reg car_mkt24 ib`mid'.xt , vce(cluster firmid);
	eststo monthly48: reg car_mkt48 ib`mid'.xt , vce(cluster firmid);

	use `rawretfile', clear;
	eststo raw_daily180: reg bhret_w180 ib`mid'.xt , vce(cluster firmid);
	eststo raw_daily360: reg bhret_w360 ib`mid'.xt , vce(cluster firmid);
	eststo raw_monthly24: reg bhret_w24 ib`mid'.xt , vce(cluster firmid);
	eststo raw_monthly48: reg bhret_w48 ib`mid'.xt , vce(cluster firmid);

	esttab * using output/table7a.csv , replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';

	* panel B;
	local controls "i.at_rankingp2003_xt i.lev_rankingp2003_xt i.cash_rankingp2003_xt";
	local mid 5;
	eststo clear;
	use `carfile', clear;
	eststo daily180: reg car_mkt180 ib`mid'.xt `controls', vce(cluster firmid);
	eststo daily360: reg car_mkt360 ib`mid'.xt `controls', vce(cluster firmid);
	eststo monthly24: reg car_mkt24 ib`mid'.xt `controls', vce(cluster firmid);
	eststo monthly48: reg car_mkt48 ib`mid'.xt `controls', vce(cluster firmid);

	use `rawretfile', clear;
	eststo raw_daily180: reg bhret_w180 ib`mid'.xt `controls', vce(cluster firmid);
	eststo raw_daily360: reg bhret_w360 ib`mid'.xt `controls', vce(cluster firmid);
	eststo raw_monthly24: reg bhret_w24 ib`mid'.xt `controls', vce(cluster firmid);
	eststo raw_monthly48: reg bhret_w48 ib`mid'.xt `controls', vce(cluster firmid);

	esttab * using output/table7b.csv , replace r2 scalars(N_clust) nobase noomit sfmt(a3) `esttabopt';


			
* figure 1;
	local tlabels "1990 1992 1994 1996 1998 2000 2002 2004 2006 2008 2010";

	use `file_fig1', clear;

	graph twoway (connected kld_diff_nogov7 year,  xline(2003, lpattern(dash) lcolor(black)))
		(connected kld_diff_nogov45483 year,  )
		(connected kld_diff_nogov_sp500 year, )
		, graphregion(color(white))
		xlabel(`tlabels')
		ylabel(,nogrid)
		scheme(s2mono)
		legend(order(1 "AAPL" 2 "GOOG" 3 "S&P 500" ) rows(1) region(lwidth(none) lpattern(blank) lstyle(none) style(none)) symxsize(6) rowgap(*.6))
		xtitle("")
		ytitle("ESG", color(black))
		name(figure1);	


* figure 2;
	local minyr 1996;
	local maxyr 2005;

	use `file', clear;

	collapse (mean) kld_diff_nogov if year >= 1999 & year <= 2006, by(year eg_slope_dd0_rankingp2003_xt);
	label var kld_diff_nogov "Total strengths minus concerns";
	format year %ty;
	xtset eg_slope_dd0_rankingp2003_xt year;
	
	graph twoway
		(tsline kld_diff_nogov if eg_slope_dd0_rankingp2003_xt == 1, tline(2003, lpattern(dash) lcolor(black)) lwidth(medthick))
		(tsline kld_diff_nogov if eg_slope_dd0_rankingp2003_xt == 5, lwidth(medthick))
		(tsline kld_diff_nogov if eg_slope_dd0_rankingp2003_xt == 3, lwidth(medthick))
		, graphregion(color(white)) 
		scheme(s2mono)
		ylabel(,nogrid)
		tlabel(1999(1)2006)
		legend(order(1 "Low (1)"  3 "Mid (3)" 2 "High (5)") rows(1) region(lwidth(none) lpattern(blank) lstyle(none) style(none)) symxsize(6) rowgap(*.6))
		xtitle("")
		name(figure2);

* figure 4;
* non-parametric analysis;
	use `file', clear;

	local year = 2003;
	local baseyear = `year'-1;

	* get corresponding mainperiodle for main regression;
	xtreg kld_diff_nogov ib2002.year##(ib3.eg_slope_dd0_rankingp2003_xt ib3.at_rankingp2003_xt) if year >= 1999 & year <= 2006, vce(cluster firmid) fe;
	keep if e(mainperiodle);
	gen t = year-2002;
	gen tleft = t + 3;
	
	* compute difference;
	* difference in year t over base year is captured via u;
	gen u = .;
	xtset firmid t;
	local i 1;
	while `i' <= 4 {;
		* s(i) good for t=(i);
		* r(i) good for t=(-i);

		gen s`i' = s`i'.kld_diff_nogov;
		gen r`i' = f`i'.kld_diff_nogov - kld_diff_nogov;

		replace u = s`i' if t == `i';
		replace u = r`i' if t == -`i';

		local i = `i' + 1;
	};

	* compute percentile ranks of ownership;
	preserve;
	collapse (mean) eg_slope_dd0_rankingp2003, by(firmid);
	egen n = count(eg_slope_dd0_rankingp2003);
	egen i = rank(eg_slope_dd0_rankingp2003), track;
	gen eg_slope_dd0_rankingp2003_p = (i-1)/(n-1);
	drop n i;
	save temp_figure4_np_ranks_p, replace;
	restore;
	merge m:1 firmid using temp_figure4_np_ranks_p, keep(master matches) assert(matches) nogen;

	local i -3;
	while `i' <= 4 {;
		local t = `baseyear'+`i';
		local tleft = `i'+3;
		if `tleft' != 3 {;

		lpoly u eg_slope_dd0_rankingp2003_p if t == `i', deg(1) name(rot);
		local rot = r(bwidth);
		local bw1 = .75*`rot';
		local bw2 = 1.25*`rot';
		graph drop rot;

		graph twoway
			(lpoly u eg_slope_dd0_rankingp2003_p, bwidth(`bw1') deg(1) lwidth(thick) lcolor(gs9) kernel(epanechnikov))
			(lpoly u eg_slope_dd0_rankingp2003_p, bwidth(`bw2') deg(1) lwidth(thick) lcolor(gs6) kernel(epanechnikov))
			(lpoly u eg_slope_dd0_rankingp2003_p, deg(1) lwidth(thick) lcolor(black) kernel(epanechnikov))
			if t == `i'
			,
			ytitle("KLD(`t') - KLD(`baseyear')")
			xtitle("Ownership (%) Percentile")
			title("`t'")
			graphregion(color(white))
			title("`t'", color(black))
			note("")
			legend(off)
			yscale(range(-1 1))
			ylabel(-1 -.5 0 .5 1,nogrid)
			name(ll_pctileepanechnikov`tleft');
		};
		local i = `i' + 1;
	};
	graph combine ll_pctileepanechnikov0 ll_pctileepanechnikov1 ll_pctileepanechnikov2
		,
		name(ll_pctileepanechnikov_pre)
		graphregion(color(white))
		title("Pre tax cut", size(small) color(black))
		iscale(*0.7)
		xcommon
		ycommon;
	graph combine ll_pctileepanechnikov4 ll_pctileepanechnikov5 ll_pctileepanechnikov6 ll_pctileepanechnikov7
		,
		name(ll_pctileepanechnikov_post)
		graphregion(color(white))
		title("Post tax cut", size(small) color(black))
		iscale(*0.7)
		xcommon
		ycommon;

	graph combine ll_pctileepanechnikov_pre ll_pctileepanechnikov_post
		,
		name(figure4)
		graphregion(color(white))
		xsize(8) ysize(5.5)
		xcommon
		ycommon
		;

	graph drop ll_*;

* figure 5, time path of CARs;
	use `cartsfile', clear;
	
	xtline car
		,
		plot1opt(lwidth(medthick)) plot2opt(lwidth(medthick)) plot3opt(lwidth(medthick)) plot4opt(lwidth(medthick)) plot5opt(lwidth(medthick))
		scheme(s2mono)
		overlay
		ylabel(#8,nogrid ticks glcolor(gs14) labsize(small))
		xlabel(, labsize(small))
		xline(0)
		graphregion(color(white)) 
		legend(label(1 "1-Low") label(2 "2") label(3 "3-Mid") label(4 "4") label(5 "5-High") rows(1) size(small) region(lwidth(none) lpattern(blank) lstyle(none) style(none)) symxsize(6) rowgap(*.6))
		ytitle("CAR", size(small)) 
		xtitle("Event time (calendar days)", size(small)) 
		name(figure5);	

log close;
